/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package questionaire;

/**
 *
 * @author balbir kaur
 */
import java.util.*;
import java.text.DecimalFormat;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import javax.servlet.ServletRequest;
        import org.apache.struts2.interceptor.RequestAware;
public class showResultsAction extends ActionSupport implements RequestAware{
   public int score=0;
   Map request;
   public String []name=new String[20];
   public TreeMap selectedQuestions=new TreeMap();
   String percentage=null;
 public TreeMap correctAnswers=new TreeMap();
 public String message;
 public int questions=0;
    @Override
   public void setRequest(Map m){
   this.request=m;
   }
    public Map getServletRequest(){
    return this.request;
    }
   @Override
   public String execute(){
       this.setCorrectAnswers();
       this.setselectedQuestions();
       this.setScore();
       this.setMessage(" ");
       return SUCCESS;
    }
   
   public void setselectedQuestions(){
        //this.selectedQuestions=(TreeMap)ServletActionContext.getRequest().getAttribute("selectedQuestions");
       Map m=ServletActionContext.getContext().getSession();
 this.selectedQuestions=(TreeMap)m.get("selectedQuestions");

    }
    public void setCorrectAnswers(){
        //this.correctAnswers=(TreeMap)ServletActionContext.getRequest().getAttribute("correctAnswers");
      Map m=ServletActionContext.getContext().getSession();
        this.correctAnswers= (TreeMap)m.get("correctAnswers");

    }
    public int getScore(){
    return(this.score);
    }
    public void setName(String name[]){
        this.name=name;
    }
    public String[] getName(){
    return this.name;
    }
   public void setScore(){
   int temp=0;
   Set keys=correctAnswers.keySet();
   Iterator it=keys.iterator();
   int i=1;
   String p;
   String c=" ";
   while(it.hasNext()){
     String item=(String)it.next();
     p=(String)correctAnswers.get(item);
     p=p.trim();
     if(name[i]!=null)
     c=this.name[i].trim();
     if(p.equals(c)){
         System.out.println(correctAnswers.get(item));
       temp=temp+1;
     }
      i=i+1;
   }
   this.score=temp;
   this.questions=i-1;
   }
public void setMessage(String message){
 Double a=this.score/(this.questions*1.0);//simple just to get the correct decimal value
 a=a*100;
 DecimalFormat formatter=new DecimalFormat("###.##");
 String op=formatter.format(a);
 this.percentage=op;
 this.message=message+this.score +"/"+this.questions+" for a percentage of "+op +"%";

}
public String getPercentage(){
return this.percentage;
}


public String getMessage(){
return this.message;
}



  }


